/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;


namespace TFrameWork.Module.ActionEditor
{
    public class SignalTrackStyle : CommonTrackStyle<SignalTrackStyle, SignalEventItemStyle>
    {
        public override CommonChildTrackBase CreateChildTrack()
        {
            return new CommonChildTrack();
        }

        public class CommonChildTrack : CommonChildTrackBase
        {
            public override void InitEvent()
            {
                if (_data == null || _data.events == null) return;

                for (int i = 0; i < _data.events.Count; i++)
                {
                    var e = _data.events[i] as SignalEvent;
                    if (e)
                    {
                        var eventItemStyle = new SignalEventItemStyle();
                        eventItemStyle.Init(this, e);
                        _eventItemStyleList.Add(eventItemStyle);
                    }
                }
            }

            protected override void InitTrackMouseMene(VisualElement trackParent)
            {
                // // 创建右键菜单
                var menu = new ContextualMenuManipulator(evt =>
                {
                    evt.menu.AppendAction("添加信号", CreateSignal, addState);
                    addState = DropdownMenuAction.Status.Normal;
                }); 

                // 将右键菜单添加到 trackRoot 上
                trackParent.AddManipulator(menu);
            }

            public virtual void CreateSignal(DropdownMenuAction obj)
            {
                var localPositon = trackRoot.WorldToLocal(obj.eventInfo.mousePosition);
                int selectFrameIndex = ActionEditor.Instance.GetFrameIndexByPos(localPositon.x);
                //判断当前帧是否已经有信号 todo 重叠提示
                var signalEvent =DataBase.New<SignalEvent>();
                signalEvent.isDuration = false;
                signalEvent.durationIndex = 0;
                signalEvent.startIndex = selectFrameIndex;
                var eventItemStyle = new SignalEventItemStyle();
                eventItemStyle.Init(this, signalEvent);

                TUndo.RegisterCreatedObjectUndo(signalEvent, "CreateSignal");
                TUndo.RecordObject(_data, "CreateSignal");
                _data.AddEvent(signalEvent);
                AssetDatabase.AddObjectToAsset(signalEvent, _data);
                EditorUtility.SetDirty(_data);
                AssetDatabase.SaveAssets();

                _eventItemStyleList.Add(eventItemStyle);
            }

        }
    }
}
